package com.wuyan.locationdog.ui.device

import android.graphics.drawable.ColorDrawable
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.GroupBeanContent
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_group_modify
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.GroupListAdapter
import com.wuyan.locationdog.databinding.DeviceGroupManageActivityBinding
import com.wuyan.locationdog.help.GroupHelp
import com.wuyan.locationdog.view.InputDialog
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DeviceGroupManageListActivity : BaseBindingActivity<DeviceGroupManageActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { GroupListAdapter(R.layout.item_device_group_list, null) }

    override fun initData() {
        if (GroupHelp.isMoveGroup) {
            setTitleText("移动分组")
            mAdapter.showCheckBtn = false
        } else {
            setTitleText("分组管理")
        }


        binding.apply {

            btnSure.setOnClickListener {
                InputDialog(mActivity) {
                    if (it.isNullOrEmpty()) {
                        customShowLong("请输入分组名称")
                        return@InputDialog
                    }
                    val map = HashMap<String, Any>()
                    map["groupName"] = it
                    mViewModel.addGroup(map)
                }.show()
            }
            btnEdit.setOnClickListener {
                InputDialog(mActivity, title = "重命名") {
                    val map = HashMap<String, Any>()
                    map["groupId"] = mAdapter.data.find { it.isSelect }?.groupId.toString()
                    map["groupName"] = it
                    mViewModel.updateGroup(map)
                }.show()
            }
            btnDelete.setOnClickListener {
                val groupBeanContent = mAdapter.data.find { it.isSelect }

                CommonDialog(
                    mActivity,
                    title = "删除分组",
                    "当前分组已有${groupBeanContent?.deviceCount}个设备，删除分组后设备将归属总分组中。"
                ) {
                    val map = HashMap<String, Any>()
                    map["groupId"] = groupBeanContent?.groupId.toString()
                    mViewModel.deleteGroup(map)
                }.show()
            }
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                val itemDecoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
                itemDecoration.setDrawable(
                    ColorDrawable(
                        ContextCompat.getColor(
                            context,
                            com.arcsoft.commonbase.R.color.divide
                        )
                    )
                )
                addItemDecoration(itemDecoration)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无分组"))

                    setOnItemClickListener { adapter, view, position ->
                        if (position == 0) {
                            return@setOnItemClickListener
                        }
                        if (GroupHelp.isMoveGroup) {
                            CommonDialog(mActivity, title = "确定移动到${data[position].groupName}吗？") {
                                val map = HashMap<String, Any>()
                                map["groupId"] = data[position].groupId
                                map["historyIdList"] =
                                    GroupHelp.moveGroupDeviceList?.map { it.deviceId }?.toMutableList() ?: ""
                                mViewModel.moveGroup(map)
                            }.show()
                            return@setOnItemClickListener
                        }
                        data.find { it.isSelect }?.isSelect = false
                        data[position].isSelect = true
                        notifyDataSetChanged()
                        bottomLayout.visible()
                    }
                }
            }


        }


    }

    override fun loadNetData() {
        super.loadNetData()
        mViewModel.getGroupList(hashMapOf())
    }

    override fun observe() {
        super.observe()
        mViewModel.anyBean.observe(this) {
            loadNetData()
            Bus.post(bus_group_modify, "")
        }
        mViewModel.removeSuccess.observe(this) {
            Bus.post(bus_group_modify, "")
            customShowLong("移动成功")
            finish()
        }
        mViewModel.groupBean.observe(this) {
            val groupBeanContents = it.content?.toMutableList()
            val sum = groupBeanContents?.map { it.deviceCount }?.sum()?:0
            groupBeanContents?.add(0, GroupBeanContent(sum, "", "全部设备", false))
            mAdapter.setNewInstance(groupBeanContents)

        }
    }

}